#include "logindialog.h"
#include "ui_logindialog.h"

#include "tools/aqp/aqp.hpp"
#include <QDebug>

LoginDialog::LoginDialog(QWidget *parent) :
    QDialog(parent, Qt::FramelessWindowHint),
    ui(new Ui::LoginDialog)
{
    ui->setupUi(this);
    initUI();
}

LoginDialog::~LoginDialog()
{
    delete ui;
}
void LoginDialog::initUI()
{
    ui->lineEdit->setText(trUtf8("管理员"));

    mainLayout = new QGridLayout;

    mainLayout->setColumnStretch(2,1);
    mainLayout->setColumnStretch(8,1);
    mainLayout->addWidget(ui->label,1,3,1,2);
    mainLayout->addWidget(ui->lineEdit,1,4,1,4);
    mainLayout->addWidget(ui->label_2,2,3,1,2);
    mainLayout->addWidget(ui->lineEdit_2,2,4,1,4);
    mainLayout->addWidget(ui->pushButton,3,3,1,2);
    mainLayout->addWidget(ui->pushButton_2,3,6,1,2);

    setLayout(mainLayout);

    connect(ui->pushButton, SIGNAL(clicked()), this ,SLOT(checkPassword()));
//    connect(ui->pushButton_2, SIGNAL(clicked()), this ,SIGNAL(quitSystem()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(doClose()));
}
QString LoginDialog::getPasswordFromFile(const char* fileName)
{
    QFile file(fileName);
    QString password;
    if(file.open(QIODevice::ReadWrite))
    {
        QTextStream stream(&file);
        password = stream.readLine();
        file.close();
    }
    else {
        emit passwordFileError();
    }
    return password;
}
void LoginDialog::checkPassword()
{
    if (ui->lineEdit_2->text().trimmed() == getPasswordFromFile("conf/password.txt")) {
        emit passwordRight();
        //this->deleteLater();
        close();
    }
    else {
        AQP::information(this,trUtf8("提示"),trUtf8("密码错误！"));
       // QMessageBox::information(this,tr("提示"),tr("密码错误！"));
    }
}
void LoginDialog::doClose()
{
    emit quitSystem();
    close();
}
